//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
//////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2004 Siemens AG
//
// Version: 1.1.2.5
// Date:    11. Jan. 2006
//////////////////////////////////////////////////////////////////////////////

#define EONS_MAX_BUF_LINES   20
#define EONS_MAX_BUF_BYTES   24
#define EONS_MAX_CRSM        16
#define EONS_MAX_PNN         10
#define EONS_MAX_OPL         64
#define EONS_MAX_OPN_LEN     24

// Enum for the state of EONS support
enum EONS_STATE {
  EONS_DISABLED = 0,
  EONS_NOT_AVAILABLE,
  EONS_ENABLED,
  EONS_EMULATED
};

//
// Handling of EONS service
//
class CEonsHandling {
public:
  CEonsHandling();
  ~CEonsHandling();
  BOOL   Init(CRilHandle* pRilDevice);
  BOOL   Deinit(void);
  BOOL   GetOperatorInfo(CRilInstanceHandle* pHandle, LPCSTR* pszLongName, LPCSTR* pszShortName);
  EONS_STATE GetEONSState() { return m_SupportType; }

private:
  BOOL         IsSupported(void);
  BOOL         IsLongName(void);
  BOOL         EnableModuleHandling(void);
  BOOL         LoadPnnTable(void);
  BOOL         LoadOplTable(void);
  BOOL         ScanPnnTable(void);
  BOOL         ScanOplTable(void);
  DWORD        GenerateATCmd(char* szBuf, DWORD dwAddr, DWORD dwStartRec, DWORD dwLength, DWORD dwMaxRecs);
  BOOL         ScanPnnName(BYTE* pSrc, char* pDst, DWORD dwLen);
  void         TraceEonsInfo(void);
  static HRESULT ParseEONSQueryData(LPCSTR szRsp, void*& pBlob, UINT& cbBlob, LPVOID pParam);
  static HRESULT ParseEONSData(LPCSTR szRsp, void*& pBlob, UINT& cbBlob, LPVOID pParam);
  static HRESULT ParseSimData(LPCSTR szRsp, void*& pBlob, UINT& cbBlob, LPVOID pParam);
  static HRESULT ParseSmoncData(LPCSTR szRsp, void*& pBlob, UINT& cbBlob, LPVOID pParam);

  // Info from EF - PNN table
  typedef struct tagPnnInfo {
    char szLongOperatorName[EONS_MAX_OPN_LEN + 1];
    char szShortOperatorName[EONS_MAX_OPN_LEN + 1];
  } PNN_INFO, *PPNN_INFO;

  // Info from EF - OPL table
  typedef struct tagOPLInfo {
    BOOL  fValid;
    BYTE  pbMCC[3];
    BYTE  pbMNC[3];
    WORD  wLACLow;
    WORD  wLACHigh;
    BYTE  bPLMN;
  } OPL_INFO, *POPL_INFO;

  HANDLE            m_hReadyEvent;
  EONS_STATE        m_SupportType;
  CRilHandle*       m_pRilDevice;
  DWORD             m_dwPnnEntries;
  DWORD             m_dwOplEntries;
  PNN_INFO          m_tPnnInfo[EONS_MAX_PNN];
  OPL_INFO          m_tOplInfo[EONS_MAX_OPL];
  CRITICAL_SECTION  m_csRunningCmd;
  DWORD             m_dwLines;
  DWORD             m_dwBytes[EONS_MAX_BUF_LINES];
  BYTE              m_bBuffer[EONS_MAX_BUF_LINES][EONS_MAX_BUF_BYTES];
  char              m_szModuleLongOperatorName[EONS_MAX_OPN_LEN + 1];
  char              m_szModuleShortOperatorName[EONS_MAX_OPN_LEN + 1];
  BOOL              m_fIsLongName;
  WORD              m_wSimSw1;
  WORD              m_wSimSw2;
  WORD              m_wCurrentMCC;
  WORD              m_wCurrentMNC;
  WORD              m_wCurrentLAC;
};

